package net.lab1024.sa.ai.stream.module.business.ai.integration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class LlmErrorCodeHandlerFactory {

    private final Map<String, LlmErrorCodeHandler> handlerMap = new HashMap<>();

    @Autowired
    public LlmErrorCodeHandlerFactory(List<LlmErrorCodeHandler> handlerList) {
        for (LlmErrorCodeHandler handler : handlerList) {
            handlerMap.put(handler.getModelType().toLowerCase(), handler);
        }
    }

    /**
     * 根据模型类型获取对应的错误码处理器
     */
    public LlmErrorCodeHandler getHandler(String modelType) {
        return handlerMap.getOrDefault(modelType.toLowerCase(), new DefaultLlmErrorCodeHandler(modelType));
    }

    /**
     * 默认处理器（兜底）
     */
    private static class DefaultLlmErrorCodeHandler implements LlmErrorCodeHandler {
        private final String modelType;

        public DefaultLlmErrorCodeHandler(String modelType) {
            this.modelType = modelType;
        }

        @Override
        public String getModelType() {
            return modelType;
        }

        @Override
        public String getFriendlyMessage(int statusCode, String errorBody) {
            return "模型[" + modelType + "] API 调用失败，错误码：" + statusCode + "，请联系管理员。";
        }
    }
} 